package com.zerowidth.db.op;

import com.zerowidth.db.DBHelper;
import com.zerowidth.db.base.SqlResult;
import com.zerowidth.db.bean.MockStatBean;
import com.zerowidth.db.bean.MockStatUserBean;
import com.zerowidth.db.utils.IOUtils;
import com.zerowidth.db.utils.StringUtils;
import com.zerowidth.db.utils.TimeUtils;

import java.sql.ResultSet;
import java.sql.SQLException;

public class MockStatUserDB {

    public static int countByCreateTime(String start_time, String end_time) {
        String sql = String.format("SELECT COUNT(*) FROM mock_stat_user WHERE create_time >= '%s' " +
                " AND create_time <= '%s'", start_time, end_time);

        return DBHelper.queryCount(sql);
    }

    public static int countByUpdateTime(String start_time, String end_time) {
        String sql = String.format("SELECT COUNT(*) FROM mock_stat_user WHERE update_time >= '%s' " +
                " AND update_time <= '%s'", start_time, end_time);

        return DBHelper.queryCount(sql);
    }

    public static boolean clearDeviceInfo(String userId) {
        String sql = String.format("UPDATE mock_stat_user SET brand = '', model = '' WHERE user_id = '%s' ", userId);

        return DBHelper.executeUpdate(sql) > 0;
    }

    public static MockStatBean findUserDB(String statId, String userId) {
        String sql = "";

        if (StringUtils.isNotEmpty(userId)) {
            sql = String.format("SELECT * FROM mock_stat_user WHERE stat_id ='%s' OR user_id = '%s'", statId, userId);
        } else {
            sql = String.format("SELECT * FROM mock_stat_user WHERE stat_id = '%s'", statId);
        }

        SqlResult sr = DBHelper.executeQueryEx(sql);
        if (sr == null) {
            return null;
        }
        if (sr.rs == null) {
            sr.close();
            return null;
        }

        MockStatBean bean = new MockStatBean();
        try {
            if (sr.rs.next()) {
                bean.stat_id = sr.rs.getString("stat_id");
                bean.user_id = sr.rs.getString("user_id");
                bean.first_use_time = sr.rs.getString("first_use_time");
                bean.user_count = sr.rs.getInt("user_count");
                bean.create_time = sr.rs.getString("create_time");
                bean.update_time = sr.rs.getString("update_time");
                bean.free_trial_end_time = sr.rs.getString("free_trial_end_time");
                bean.brand = sr.rs.getString("brand");
                bean.model = sr.rs.getString("model");
                bean.os_version = sr.rs.getString("os_version");
                bean.share_count = sr.rs.getInt("share_count");

                return bean;
            }
        } catch (SQLException sqlException) {
            sqlException.printStackTrace();
        }

        return null;
    }

    public static boolean insertDB(String statId, String userId, String first_use_time, String create_time,
                                   String update_time, String free_trial_end_time,
                                   String brand, String model, String manufacturer,
                                   String os_version, String appVersionInit, String app_channel, String client_os,
                                   String ip, String appSignKeyMd5) {

        String sql = String.format("INSERT INTO mock_stat_user(stat_id, user_id, first_use_time, user_count, " +
                        "create_time, update_time, free_trial_end_time, brand, model, manufacturer, os_version, " +
                        "app_version_init, app_channel, client_os, credit, bonus, game_run_count, ip, app_sign_key) " +
                        "VALUES('%s', '%s', '%s', 0, '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s'," +
                        " 600, 0, 0, '%s', '%s')",
                statId, userId, first_use_time, create_time, update_time, free_trial_end_time,
                brand, model, os_version, manufacturer, appVersionInit, app_channel, client_os, ip, appSignKeyMd5);

        return DBHelper.executeInsert(sql) > 0;
    }

    public static boolean updateUserIdByStatId(String statId, String userId, String appSignKeyMd5) {

        String time = TimeUtils.convert(System.currentTimeMillis(), TimeUtils.TIME_FORMAT_YYYY_MM_DD_HH_MM_SS);
        String sql = String.format("UPDATE mock_stat_user SET user_id = '%s', update_time = '%s', app_sign_key = '%s' " +
                "WHERE stat_id = '%s'", userId, time, appSignKeyMd5, statId);

        return DBHelper.executeUpdate(sql) > 0;
    }

    //根据 statID 来更新统计信息
    public static boolean updateUserStat(String statId,
                                         String appVersionInit, String app_channel, String client_os,
                                         String ip, String appSignKeyMd5) {
        String time = TimeUtils.convert(System.currentTimeMillis(), TimeUtils.TIME_FORMAT_YYYY_MM_DD_HH_MM_SS);
        String sql = "";

        sql = String.format("UPDATE mock_stat_user SET user_count = user_count +1, update_time = '%s', " +
                " app_version_now = '%s', app_channel = '%s', client_os = '%s', ip = '%s', app_sign_key = '%s'" +
                " WHERE stat_id = '%s'", time, appVersionInit, app_channel, client_os, ip, appSignKeyMd5, statId);

        return DBHelper.executeUpdate(sql) > 0;
    }

    //根据 userID 来更新统计信息
    public static boolean updateUserStat2(String userId, String brand, String model,
                                          String appVersion, String app_channel, String client_os,
                                          String ip, String appSignKeyMd5) {
        String time = TimeUtils.convert(System.currentTimeMillis(), TimeUtils.TIME_FORMAT_YYYY_MM_DD_HH_MM_SS);
        String sql = "";

        sql = String.format("UPDATE mock_stat_user SET user_count = user_count +1, update_time = '%s', " +
                " brand = '%s', model = '%s', " +
                " app_version_now = '%s', app_channel = '%s', client_os = '%s', ip = '%s', app_sign_key = '%s'" +
                " WHERE user_id = '%s'", time, brand, model, appVersion, app_channel, client_os, ip, appSignKeyMd5, userId);

        return DBHelper.executeUpdate(sql) > 0;
    }

    public static boolean modifyShareAward(String statId, String userId) {
        String time = TimeUtils.convert(System.currentTimeMillis(), TimeUtils.TIME_FORMAT_YYYY_MM_DD_HH_MM_SS);
        String sql = "";

        String freeTrialEndTime = TimeUtils.convert(System.currentTimeMillis() + 24 * 60 * 60 * 1000,
                TimeUtils.TIME_FORMAT_YYYY_MM_DD_HH_MM_SS);

        sql = String.format("UPDATE mock_stat_user SET share_count = share_count +1, update_time = '%s'," +
                " free_trial_end_time = '%s' " +
                " WHERE stat_id = '%s'", time, freeTrialEndTime, statId);

        return DBHelper.executeUpdate(sql) > 0;
    }

    public static MockStatUserBean findByStatId(String statId) {

        String sql = String.format("SELECT * FROM mock_stat_user WHERE stat_id = '%s' ", statId);

        SqlResult sr = DBHelper.executeQueryEx(sql);


        try {
            if (sr == null) {
                return null;
            }
            ResultSet rs = sr.rs;
            if (rs.next()) {

                MockStatUserBean bean = new MockStatUserBean();

                bean.user_id = rs.getString("user_id");

                return bean;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            IOUtils.close(sr);
        }

        return null;
    }

    public static boolean addRunCount(String statId) {
        String sql = String.format("UPDATE mock_stat_user SET game_run_count = game_run_count + 1 " +
                "WHERE stat_id = '%s'", statId);

        return DBHelper.executeUpdate(sql) > 0;
    }

    public static boolean reduceCredit(String statId, int costCredit) {

        String sql = String.format("UPDATE mock_stat_user SET credit = credit - %d " +
                "WHERE stat_id = '%s'", costCredit, statId);


        return DBHelper.executeUpdate(sql) > 0;
    }

    public static boolean updateBetMax(String statId, int betMax) {
        String sql = String.format("UPDATE mock_stat_user SET bet_max = %d " +
                "WHERE stat_id = '%s'", betMax, statId);

        return DBHelper.executeUpdate(sql) > 0;
    }

    public static boolean bonusChange(String statId, int bonus, int creditDelta) {

        String sql = String.format("UPDATE mock_stat_user SET bonus = %d , credit = credit + %d " +
                "WHERE stat_id = '%s'", bonus, creditDelta, statId);


        return DBHelper.executeUpdate(sql) > 0;
    }

    public static boolean addCredit(String statId, int addCount) {

        String sql = String.format("UPDATE mock_stat_user SET credit = credit + %d " +
                "WHERE stat_id = '%s'", addCount, statId);

        return DBHelper.executeUpdate(sql) > 0;
    }

    public static boolean transfer(String statId, int count) {
        String sql = String.format("UPDATE mock_stat_user SET bonus = bonus + %d , credit = credit - %d " +
                "WHERE stat_id = '%s'", count, count, statId);

        return DBHelper.executeUpdate(sql) > 0;
    }

    public static boolean setGameCoinByStatId(String statId, int coinNum) {

        String sql = String.format("UPDATE mock_stat_user SET credit = %d " +
                "WHERE stat_id = '%s'", coinNum, statId);


        return DBHelper.executeUpdate(sql) > 0;
    }

    public static boolean addGameCoin(String stat_id, int coinNum) {

        String sql = String.format("UPDATE mock_stat_user SET credit = credit + %d " +
                "WHERE stat_id = '%s'", coinNum, stat_id);


        return DBHelper.executeUpdate(sql) > 0;
    }

}





















